package com.tc.nio;

import com.tc.util.ByteBufferUtil;

import java.nio.ByteBuffer;

/**
 * @Auther: tianchao
 * @Date: 2022/1/29 16:19
 * @Description:
 */
public class TestByteBufferRead {
    public static void main(String[] args) {
        //testRewind();
        // mark 做标记 记录 position位置 rest是将position重置到mark的位置
        testMarkReset();

    }

    public static void testMarkReset(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put(new byte[]{'a','b','c','d'});
        byteBuffer.flip();
        byteBuffer.get();
        ByteBufferUtil.debugAll(byteBuffer);
        // 加标记， position 是1 的位置
        byteBuffer.mark();
        byteBuffer.get();
        byteBuffer.get();
        ByteBufferUtil.debugAll(byteBuffer);
        //重置后，position再次回到1
        byteBuffer.reset();
        ByteBufferUtil.debugAll(byteBuffer);
    }

    /**
     * rewind 将position置为0
     */
    public static void testRewind(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put(new byte[]{'a','b','c','d'});
        byteBuffer.flip();
        ByteBuffer byteBuffer1 = byteBuffer.get(new byte[4]);
        ByteBufferUtil.debugAll(byteBuffer);
        byteBuffer.rewind();
        ByteBufferUtil.debugAll(byteBuffer);
        byte b = byteBuffer.get();
        System.out.println((char) b);
        ByteBufferUtil.debugAll(byteBuffer);
    }
}
